#!/usr/bin/env python
#coding:utf-8
#
# Copyright 2009 CoderZh.com.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = 'CoderZh'

import logging

from google.appengine.api import users

from common.debug import set_trace
import common.authorized as authorized
from common.config import *
from common.view import BaseRequestHandler
from models import *

class MainPage(BaseRequestHandler):
    def get(self):
        self.template_render('index.html')


class QuickAddShortcut(BaseRequestHandler):
    @authorized.role('user')
    def post(self):
        try:
            link = self.request.POST.get('link')
            new_shortcut = Shortcuts.create(link)
            self.template_render('add_result.html', {'new_shortcut': new_shortcut})
        except Exception,e:
            logging.error('Quick Add Shortcut failed! error: %s' % e)
            self.response.out.write('{}')
            
class AddShortcut(BaseRequestHandler):
    @authorized.role('user')
    def get(self):
        self.template_render('add.html')

    @authorized.role('user')
    def post(self):
        try:
            shortname = self.request.POST.get('shortname')
            longname = self.request.POST.get('longname')
            category_id = self.request.POST.get('category_id')
            link = self.request.POST.get('link')
            picture = self.request.POST.get('picture')
            category = None
            if category_id:
                category = Category.get_by_current_user(category_id)
                
            new_shortcut = Shortcuts.create(shortname=shortname, longname=longname, link=link, picture=picture, category=category)
            self.response.out.write(R_OK)
        except Exception, e:
            logging.error('Add Shortcut failed! error: %s' % e)
            self.response.out.write(R_ERROR)


class EditShortcut(BaseRequestHandler):
    @authorized.role('user')
    def get(self):
        try:
            shortcut_id = self.request.GET.get('shortcut_id')
            shortcut = Shortcuts.get_by_current_user(s_id)
            if not shortcut:
                raise 'Shortcut not found'
            
            values = { 'shortcut' : shortcut }
            self.template_render('edit.html', values)
        except Exception, e:
            logging.error('Edit Shortcut failed! error: %s' % e)
            self.redirect('/error?msg=%s' % e)

    @authorized.role('user')
    def post(self):
        try:
            shortcut_id = self.request.POST.get('shortcut_id')
            shortname = self.request.POST.get('shortname')
            longname = self.request.POST.get('longname')
            category_id = sellf.request.POST.get('category_id')
            link = self.request.POST.get('link')
            picture = self.request.POST.get('picture')

            category = Category.get_by_current_user(category_id)

            edit_shortcut = Shortcuts.edit(id=shortcut_id, shortname=shortname, longname=longname, link=link, picture=picture, category=category)
            self.response.out.write(R_OK)
        except Exception, e:
            logging.error('Edit Shortcut failed, error:%s' % e)
            self.response.out.write(R_ERROR)


class DeleteShortcut(BaseRequestHandler):
    @authorized.role('user')
    def post(self):
        try:
            shortcut_id = self.request.POST.get('id')
            Shortcuts.delete(shortcut_id)
            self.template_render('result.html', { 'result':True })
        except Exception, e:
            logging.error('Delete Shortcut failed, error:%s' % e)
            self.template_render('result.html', { 'result':False })


class AddCategory(BaseRequestHandler):
    @authorized.role('user')
    def get(self):
        self.template_render('add_cateory.html')

    @authorized.role('user')
    def post(self):
        try:
            name = self.request.POST.get('name')
            description = self.request.POST.get('description')
            parent_id  = self.request.POST.get('parent_id')
            parent = None
            if parent_id:
                parent = Category.get_by_current_user(parent_id)
            Category.create(name=name, description=description, parent=parent)
            self.response.out.write(R_OK)
        except Exception, e:
            logging.error('Add Category failed, error:%s' % e)
            self.redirect('/error?msg=%s' % e)


class EditCategory(BaseRequestHandler):
    @authorized.role('user')
    def get(self):
        try:
            id = self.request.GET.get('id')
            category = Category.get_by_current_user(id)
            if not category:
                raise 'Category not found'
            
            values = { 'category': category }
            self.template_render('edit_category.html',values)
        except Exception, e:
            logging.error('Edit Category failed, error:%s' % e)
            self.redirect('/error?msg=%s' % e)

    @authorized.role('user')
    def post(self):
        try:
            id = self.request.POST.get('id')
            name = self.request.POST.get('name')
            description = self.request.POST.get('description')
            parent_id = self.request.POST.get('parent_id')
            parent = None
            if parent_id:
                parent = Category.get_by_current_user(parent_id)
                
            Category.edit(id=id, name=name, description=description, parent=parent)
            self.response.out.write(R_OK)
        except Exception, e:
            logging.error('Post Edit Category failed, error:%s' % e)
            self.redirect('error?msg=%s' % e)


class DeleteCategory(BaseRequestHandler):
    @authorized.role('user')
    def post(self):
        try:
            id = self.request.POST.get('id')
            Category.delete(id)
        except Exception, e:
            logging.error('Delete Category failed, error:%s' % e)
            self.redirect('error?msg=%s' % e)


class MoveShortcut(BaseRequestHandler):
    @authorized.role('user')
    def post(self):
        try:
            id = self.request.POST.get('id')
            left = self.request.POST.get('left')
            top = self.request.POST.get('top')
            Shortcuts.move(id, left, top)
        except Exception, e:
            logging.error('Move Shortcut failed, error:%s' % e)
